package com.eduhzy.ttw.teacher.mvp.presenter;

import android.app.Application;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.entity.RegisterData;
import com.eduhzy.ttw.teacher.mvp.contract.ForgetPWDContract;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.RxLifecycleUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


@ActivityScope
public class ForgetPWDPresenter extends BasePresenter<ForgetPWDContract.Model, ForgetPWDContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public ForgetPWDPresenter(ForgetPWDContract.Model model, ForgetPWDContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void requestResetPWD(String phone, String pwd, String code) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("mobile", phone);
        map.put("passWord", pwd);
        map.put("code", code);
        mModel.requestResetPWD(map)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3, 2))
                .doOnSubscribe(disposable -> mRootView.showLoading())
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<RegisterData>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<RegisterData> commonData) {
                        if (commonData.isResult()) {
                            SPUtils.getInstance().put(Constants.USERNAME, phone);
                            SPUtils.getInstance().put(Constants.PASSWORD, "");
                            RxUtil.showTipDialog(ActivityUtils.getTopActivity(), QMUITipDialog.Builder.ICON_TYPE_SUCCESS, "密码修改成功！", 1500);
                            ARouter.getInstance().build(RouterHub.APP_LOGINACTIVITY).withBoolean(AppManager.IS_NOT_ADD_ACTIVITY_LIST, true).navigation();
                            ArmsUtils.killAll();
                        }
                    }
                });
    }
}
